package com.example.yskjproject;


import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.View;

import androidx.appcompat.widget.AppCompatTextView;
import androidx.core.content.FileProvider;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.android.tu.loadingdialog.LoadingDailog;
import com.bumptech.glide.Glide;
import com.example.yskjproject.adapter.LeftMenuAdapter;
import com.example.yskjproject.base.BaseActivity;
import com.example.yskjproject.content.EventContent;
import com.example.yskjproject.databinding.MainLayoutBinding;
import com.example.yskjproject.event.EventMessage;
import com.example.yskjproject.helper.USBSerialHelper;
import com.example.yskjproject.helper.interfaces.ActivityCallback;
import com.example.yskjproject.http.APIService;
import com.example.yskjproject.http.Urls;
import com.example.yskjproject.http.callback.JsonCallback;
import com.example.yskjproject.model.LeftMenuModel;
import com.example.yskjproject.model.LeftModel;
import com.example.yskjproject.model.LzyResponse;
import com.example.yskjproject.model.UserInfoDataModel;
import com.example.yskjproject.model.VerityVersionModel;
import com.example.yskjproject.service.BottomDiscService;
import com.example.yskjproject.ui.fragment.AllGameFragment;
import com.example.yskjproject.ui.fragment.ContentFragment;
import com.example.yskjproject.ui.fragment.FitnessCollegeFragment;
import com.example.yskjproject.ui.fragment.FollowFragment;
import com.example.yskjproject.ui.fragment.MessageFragment;
import com.example.yskjproject.ui.fragment.NumberFragment;
import com.example.yskjproject.ui.fragment.OrderInfoFragment;
import com.example.yskjproject.ui.fragment.UseGettingStartedFragment;
import com.example.yskjproject.ui.fragment.UserInfoFragment;
import com.example.yskjproject.ui.fragment.WalletFragment;
import com.example.yskjproject.utils.CashUtil;
import com.example.yskjproject.utils.DateUtils;
import com.example.yskjproject.utils.EventBusUtil;
import com.example.yskjproject.utils.Log;
import com.example.yskjproject.utils.LogUtils;
import com.example.yskjproject.utils.LoginDialogShowUtil;
import com.example.yskjproject.utils.SpCashUtils;
import com.example.yskjproject.view.CustomHorizontalProgresNoNum;
import com.example.yskjproject.view.dialog.ExitDialog;
import com.example.yskjproject.view.dialog.WorkDialogHelper;
import com.google.common.collect.Lists;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.tencent.qcloud.tuicore.util.ToastUtil;
import com.xzh.imagepicker.ImagePicker;
import com.xzh.imagepicker.bean.MediaFile;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.io.Serializable;
import java.lang.reflect.Type;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import updater.AppUpdater;
import updater.callback.AppUpdateCallback;

/**
 * 入口
 */
public class MainActivity extends BaseActivity implements USBSerialHelper.OnDataReceivedListener, ActivityCallback {

    private MainLayoutBinding binding;
    private FragmentTransaction fragmentTransaction;
    //带有筛选条件的fragment
    private ContentFragment fragmentContentData;
    private FitnessCollegeFragment fitnessCollegeFragment;
    private UseGettingStartedFragment useGettingStartedFragment;

    private UserInfoFragment userInfoFragment;
    private MessageFragment messageFragment;

    private NumberFragment numberFragment;

    private AllGameFragment allGameFragment;


    private OrderInfoFragment orderInfoFragment;
    private FollowFragment followFragment;
    private WalletFragment walletFragment;

    private LeftMenuAdapter leftMenuAdapter;

    private List<LeftMenuModel> leftData = Lists.newArrayList();

    private String homeImgType = "exit";
    private boolean peopleBoolean = true;
    private int mLastProgress = -1;
    private long mLastTime;
    private Uri apkUri;
    private LoadingDailog.Builder loadBuilder;
    private LoadingDailog loadingDailog;


    @Override
    protected boolean shouldSetBackground() {
        return false;
    }

    @Override
    protected MainLayoutBinding getViewBinding() {
        binding = MainLayoutBinding.inflate(getLayoutInflater());
        binding.rlRoot.setBackground(null);
        return binding;
    }

    @Override
    protected boolean shouldShowTitle() {
        return false;
    }

    @Override
    protected void onResume() {
        super.onResume();
        setActivityCallback(this);
        getUserInfo();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void MessageEvent(EventMessage eventMsg) {
        switch (eventMsg.getMessage()) {
            case EventContent.Login_Update:
                getUserInfo();
                show(0, leftData.get(0).getId());
                break;
            case EventContent.update_photo:
                Glide.with(MainActivity.this).load(eventMsg.getDate()).circleCrop().into(binding.userLayout);
                break;
            case EventContent.look_order://查看订单
                show(1, -1);
                break;
            case EventContent.go_home:
                disBg(0);
                getUserInfo();
                show(0, leftData.get(0).getId());
                leftData.get(0).setSelect(true);
                leftMenuAdapter.notifyDataSetChanged();
                backNull();
                homeImgType = "exit";
                Glide.with(MainActivity.this).load(R.drawable.exit_img)
                        .dontAnimate()
                        .into(binding.exitImg);
                Glide.with(MainActivity.this).load(R.mipmap.no_login).circleCrop().into(binding.userLayout);
                break;
        }
    }

    @Override
    protected boolean isRegisterEventBus() {
        return true;
    }

    @SuppressLint("CheckResult")
    @Override
    protected void initData() {
        //检测tk是否要更新
//        getTkTime();
        setLeftRvMenuDate();
        onClickButtonShow();
        verifyVersion();
        startBottomDisc();
    }

    private void startBottomDisc() {
        if (Settings.canDrawOverlays(this)) {
            // 权限已经被授予，可以开始服务
            startService(new Intent(this, BottomDiscService.class));
        } else {
            // 请求用户授予权限
            Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                    Uri.parse("package:" + getPackageName()));
            startActivityForResult(intent, 100);  // 100 是请求代码，您可以根据需要调整
        }

    }




    private void showUpdateProgress(String url) {
        AppUpdater appUpdater = new AppUpdater(this, url);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        View dialogView = getLayoutInflater().inflate(R.layout.version_update_dialog, null);
        builder.setView(dialogView);
        final AlertDialog dialogProgress = builder.create();
        dialogProgress.setCancelable(false);

        CustomHorizontalProgresNoNum pb = dialogView.findViewById(R.id.pb);
        AppCompatTextView tvDownloadProgress = dialogView.findViewById(R.id.download_progress);
        AppCompatTextView tvCancel = dialogView.findViewById(R.id.tv_version_update_cancel);
        AppCompatTextView tvInstall = dialogView.findViewById(R.id.tv_install);

        dialogProgress.show();
        appUpdater.setUpdateCallback(new AppUpdateCallback() {
            @Override
            public void onProgress(long progress, long total, boolean isChange) {
                long curTime = System.currentTimeMillis();
                if (mLastTime + 200 < curTime) {//降低更新频率
                    mLastTime = curTime;

                    int currProgress = Math.round(progress * 1.0f / total * 100.0f);
                    if (currProgress != mLastProgress) {//百分比改变了才更新

                        pb.setProgress(currProgress);
                        pb.setMax(100);
                        tvDownloadProgress.setText("正在下载： " + currProgress + "%");
                        LogUtils.d("progress:::" + currProgress + "");
                        mLastProgress = currProgress;
                    }
                }
            }

            @Override
            public void onFinish(File file) {
//                tvCancel.setVisibility(View.GONE);
                tvInstall.setVisibility(View.VISIBLE);
                apkUri = FileProvider.getUriForFile(getApplicationContext(), "com.example.yskjproject.FileProvider", file);
            }
        });
        appUpdater.start();
        tvCancel.setOnClickListener(view -> {
            if (dialogProgress != null && dialogProgress.isShowing()) {
                appUpdater.stop();
                dialogProgress.dismiss();
            }

        });
        tvInstall.setOnClickListener(view -> {
            promptUserToInstall(apkUri);
            dialogProgress.dismiss();
        });
    }

    private void promptUserToInstall(Uri uri) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "application/vnd.android.package-archive");
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

    private void verifyVersion() {
        OkGo.<LzyResponse<VerityVersionModel>>get(Urls.VERSION_CHECK).execute(new JsonCallback<LzyResponse<VerityVersionModel>>() {
            @Override
            public void onSuccess(Response<LzyResponse<VerityVersionModel>> response) {
                try {
                    // 获取PackageManager实例
                    PackageManager packageManager = getPackageManager();
                    // 获取当前应用的包信息
                    PackageInfo packageInfo = packageManager.getPackageInfo(getPackageName(), 0);
                    // 获取versionCode
                    int versionCode = packageInfo.versionCode;
                    // 获取versionName
                    String versionName = packageInfo.versionName;
                    // 打印或使用这两个版本号
                    LogUtils.d("VersionInfo" + "VersionCode: " + versionCode + ", VersionName: " + versionName);
                    if (!response.body().data.getNewApp().getVersion().equals(versionName + "")) {
                        ToastUtil.toastLongMessage("检测到新版本，请及时更新");
                        new AlertDialog.Builder(MainActivity.this)
                                .setTitle("版本更新")//设置对话框标题
                                .setIcon(android.R.drawable.ic_menu_info_details)
                                .setMessage("检测到新版本")
                                .setPositiveButton("立即下载", (dialogInterface, i) -> {
                                    showUpdateProgress(response.body().data.getNewApp().getLink());
                                    dialogInterface.dismiss();
                                })
                                .setCancelable(false)
                                .show();
                    }

                } catch (PackageManager.NameNotFoundException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onError(Response<LzyResponse<VerityVersionModel>> response) {
                super.onError(response);
            }
        });
    }

    private void getTkTime() {
        if (!TextUtils.isEmpty(SpCashUtils.getInstance().getToken())) {
            //登录的时候的日期
            String longinCalendar = SpCashUtils.getInstance().getCurrentTokenTime();
            Calendar startData = DateUtils.getNowData(longinCalendar);
            //当前日期
            Calendar endData = DateUtils.getNowData(new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance()));
            //相差天数
            long day = DateUtils.daysBetween(startData, endData);
            //得到过期的限制天数
            SpCashUtils.getInstance().getTokenTime();
//            if(){
//
//            }
        }
    }

    /**
     * 哪些fragment显示
     */
    @SuppressLint("NotifyDataSetChanged")
    private void onClickButtonShow() {
        binding.exitImg.setOnClickListener(home -> {
            switch (homeImgType) {
                case "exit":
                    ExitDialog exitDialog = new ExitDialog(MainActivity.this, "exit");
                    exitDialog.show();
                    break;
                case "home":
                    disBg(0);
                    leftData.get(0).setSelect(true);
                    leftMenuAdapter.notifyDataSetChanged();
                    show(0, leftData.get(0).getId());
                    backNull();
                    homeImgType = "exit";
                    Glide.with(MainActivity.this).load(R.drawable.exit_img)
                            .dontAnimate()
                            .into(binding.exitImg);
                    break;
            }


        });
        binding.fitnessCollegeTv.setOnClickListener(v -> show(1, -1));
        binding.useGettingStartedTv.setOnClickListener(v -> show(2, -1));
        binding.userLayout.setOnClickListener(v -> {
            for (LeftMenuModel leftMenuModel : leftData) {
                leftMenuModel.setSelect(false);
            }
            if (LoginDialogShowUtil.dialogShow(MainActivity.this)) {
                show(3, -1);
            }
        });
        binding.followCl.setOnClickListener(follow -> {
            if (!LoginDialogShowUtil.dialogShow(MainActivity.this)) {
                return;
            }
            show(4, -1);
        });
        binding.messageTv.setOnClickListener(v -> {
            if (!LoginDialogShowUtil.dialogShow(MainActivity.this)) {
                return;
            }
            show(5, -1);
        });
        binding.orderTv.setOnClickListener(view -> {
            if (!LoginDialogShowUtil.dialogShow(MainActivity.this)) {
                return;
            }
            show(6, -1);
        });
        binding.wallet.setOnClickListener(view -> {
            if (!LoginDialogShowUtil.dialogShow(MainActivity.this)) {
                return;
            }
            show(9, -1);
        });
        binding.btnNumRs.setOnClickListener(num -> show(7, -1));
        binding.allGame.setOnClickListener(game -> show(8, -1));


    }

    /**
     * 设置左侧菜单内容
     */
    private void setLeftRvMenuDate() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        binding.leftMenu.setLayoutManager(layoutManager);
        leftMenuAdapter = new LeftMenuAdapter(R.layout.item_main_left, leftData, this);
        binding.leftMenu.setAdapter(leftMenuAdapter);
        new Thread(this::getLeftData).start();
    }

    /**
     * 获取左侧标签
     */
    private void getLeftData() {
        Type type = new TypeToken<LzyResponse<LeftModel>>() {
        }.getType();
        APIService.<LzyResponse<LeftModel>>getData(type, Urls.LIST_LEVEL + "?level=1").subscribeOn(Schedulers.io()).map(response -> response.data).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<LeftModel>() {
            @Override
            public void onSubscribe(Disposable d) {
                Log.e("onSubscribe ： " + d);
            }

            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void onNext(LeftModel leftModel) {
                if (leftModel.getTag_list().isEmpty()) {
                    return;
                }
                for (LeftMenuModel leftMenuModel : leftData) {
                    leftMenuModel.setSelect(false);
                }
                leftData.clear();
                leftData.addAll(leftModel.getTag_list());
                if (!leftData.isEmpty()) {
                    leftData.get(0).setSelect(true);
                }
                leftMenuAdapter.setOnItemChildClickListener((adapter, view, position) -> {
                    for (LeftMenuModel leftMenuModel : leftData) {
                        leftMenuModel.setSelect(false);
                    }
                    leftData.get(position).setSelect(true);
                    if (position == 0) {
                        homeImgType = "exit";
                        Glide.with(MainActivity.this).load(R.drawable.exit_img)
                                .dontAnimate()
                                .into(binding.exitImg);
                    } else {
                        homeImgType = "home";
                        Glide.with(MainActivity.this).load(R.mipmap.home_icon)
                                .dontAnimate()
                                .into(binding.exitImg);
                    }
                    backNull();
                    leftMenuAdapter.notifyDataSetChanged();
                    show(0, leftData.get(position).getId());
                });
                leftMenuAdapter.notifyDataSetChanged();
                show(0, leftData.get(0).getId());
            }


            @Override
            public void onError(Throwable e) {
                e.printStackTrace();            //请求失败
                Log.e("onError ： " + "获取失败");
            }

            @Override
            public void onComplete() {

            }
        }); //
    }

    /**
     * 选择 列表的时候  下面的都是未选中
     */
    @SuppressLint({"NewApi", "UseCompatLoadingForDrawables"})
    private void backNull() {
        binding.fitnessCollegeTv.setBackground(this.getDrawable(R.color.back_main));
        binding.fitnessCollegeTv.setTextColor(this.getColor(R.color.gray_text1));
        binding.useGettingStartedTv.setBackground(this.getDrawable(R.color.back_main));
        binding.useGettingStartedTv.setTextColor(this.getColor(R.color.gray_text1));
        binding.btnNumRs.setBackground(this.getDrawable(R.color.back_main));
        binding.btnNumRs.setTextColor(this.getColor(R.color.gray_text1));
        binding.allGame.setBackground(this.getDrawable(R.color.back_main));
        binding.allGame.setTextColor(this.getColor(R.color.gray_text1));
        //底部
        binding.followCl.setBackground(this.getDrawable(R.drawable.cirecle_back));
        binding.messageTv.setBackground(this.getDrawable(R.drawable.cirecle_back));
        binding.orderTv.setBackground(this.getDrawable(R.drawable.cirecle_back));
    }


    /**
     * 显示某一页fragment
     *
     * @param page
     * @param level1Id
     */
    public void show(int page, int level1Id) {
        switch (page) {
            case 0://筛选相关
                if (level1Id != -1) {
                    setContentFragment(level1Id);
                    break;
                }
                break;
            case 1://
                disBg(1);
                setFitnessCollegeFragment();
                homeImgType = "home";
                Glide.with(MainActivity.this).load(R.mipmap.home_icon)
                        .dontAnimate()
                        .into(binding.exitImg);
                break;
            case 2://使用入门
                disBg(2);
                setUseGettingStartedFragment();
                homeImgType = "home";
                Glide.with(MainActivity.this).load(R.mipmap.home_icon)
                        .dontAnimate()
                        .into(binding.exitImg);
                break;
            case 3://我的设置
                disBg(3);
                setUserInfoFragment();
                homeImgType = "home";
                Glide.with(MainActivity.this).load(R.mipmap.home_icon)
                        .dontAnimate()
                        .into(binding.exitImg);
                break;
            case 4://我的关注
                disBg(4);
                setFollowInfoFragment();
                homeImgType = "home";
                Glide.with(MainActivity.this).load(R.mipmap.home_icon)
                        .dontAnimate()
                        .into(binding.exitImg);
                break;
            case 5://消息列表
                if (LoginDialogShowUtil.dialogShow(MainActivity.this)) {
                    disBg(5);
                    setMessageInfoFragment();
                    homeImgType = "home";
                    Glide.with(MainActivity.this).load(R.mipmap.home_icon)
                            .dontAnimate()
                            .into(binding.exitImg);
                }

                break;
            case 6: //查看订单
                if (LoginDialogShowUtil.dialogShow(MainActivity.this)) {
                    disBg(6);
                    setOrderInfoFragment();
                    homeImgType = "home";
                    Glide.with(MainActivity.this).load(R.mipmap.home_icon)
                            .dontAnimate()
                            .into(binding.exitImg);
                }
                break;
            case 7://数字原身
                disBg(7);
                setNumFragment();
                homeImgType = "home";
                Glide.with(MainActivity.this).load(R.mipmap.home_icon)
                        .dontAnimate()
                        .into(binding.exitImg);
                break;
            case 8: //
                disBg(8);
                setAllGameFragment();
                homeImgType = "home";
                Glide.with(MainActivity.this).load(R.mipmap.home_icon)
                        .dontAnimate()
                        .into(binding.exitImg);
                break;
            case 9:
                if (LoginDialogShowUtil.dialogShow(MainActivity.this)) {
                    disBg(9);
                    setWalletFragment();
                    Glide.with(MainActivity.this).load(R.mipmap.home_icon)
                            .dontAnimate()
                            .into(binding.exitImg);
                }
                break;
        }
    }

    /**
     * 设置筛选fragment
     */
    private void setContentFragment(int level1Id) {
        fragmentTransaction = getSupportFragmentManager().beginTransaction();
//        if (fragmentContentData == null) {
        fragmentContentData = new ContentFragment(level1Id);
//        }
        fragmentTransaction.replace(binding.fragLayout.getId(), fragmentContentData);
        fragmentTransaction.commit();
    }

    /**
     * 数字原身
     */
    private void setNumFragment() {
        fragmentTransaction = getSupportFragmentManager().beginTransaction();
        if (numberFragment == null) {
            numberFragment = new NumberFragment();
        }
        fragmentTransaction.replace(binding.fragLayout.getId(), numberFragment);
        fragmentTransaction.commit();
    }

    private void setAllGameFragment() {
        fragmentTransaction = getSupportFragmentManager().beginTransaction();
        if (allGameFragment == null) {
            allGameFragment = new AllGameFragment();
        }
        fragmentTransaction.replace(binding.fragLayout.getId(), allGameFragment);
        fragmentTransaction.commit();
    }


    /**
     * 健身学院
     */
    private void setFitnessCollegeFragment() {
        fragmentTransaction = getSupportFragmentManager().beginTransaction();
        if (fitnessCollegeFragment == null) {
            fitnessCollegeFragment = new FitnessCollegeFragment();
        }
        fragmentTransaction.replace(binding.fragLayout.getId(), fitnessCollegeFragment);
        fragmentTransaction.commit();
    }

    /**
     * 使用入门
     */
    private void setUseGettingStartedFragment() {
        fragmentTransaction = getSupportFragmentManager().beginTransaction();
        if (useGettingStartedFragment == null) {
            useGettingStartedFragment = new UseGettingStartedFragment();
        }
        fragmentTransaction.replace(binding.fragLayout.getId(), useGettingStartedFragment);
        fragmentTransaction.commit();
    }

    /**
     * 个人信息
     */
    private void setUserInfoFragment() {
        fragmentTransaction = getSupportFragmentManager().beginTransaction();
        if (userInfoFragment == null) {
            userInfoFragment = new UserInfoFragment();
        }
        fragmentTransaction.replace(binding.fragLayout.getId(), userInfoFragment);
        fragmentTransaction.commit();
    }

    /**
     * 关注
     */
    private void setFollowInfoFragment() {
        fragmentTransaction = getSupportFragmentManager().beginTransaction();
        if (followFragment == null) {
            followFragment = new FollowFragment();
        }
        fragmentTransaction.replace(binding.fragLayout.getId(), followFragment);
        fragmentTransaction.commit();

    }

    /**
     * 消息推送信息
     */
    private void setMessageInfoFragment() {
        fragmentTransaction = getSupportFragmentManager().beginTransaction();
        if (messageFragment == null) {
            messageFragment = new MessageFragment();
        }
        fragmentTransaction.replace(binding.fragLayout.getId(), messageFragment);
        fragmentTransaction.commit();

    }

    /**
     * 我的订单fragment
     */
    private void setOrderInfoFragment() {
        fragmentTransaction = getSupportFragmentManager().beginTransaction();
        orderInfoFragment = new OrderInfoFragment();
        fragmentTransaction.replace(binding.fragLayout.getId(), orderInfoFragment);
        fragmentTransaction.commit();

    }

    /**
     * 我的钱包fragment
     * */

    public void setWalletFragment() {
        fragmentTransaction = getSupportFragmentManager().beginTransaction();
        walletFragment = new WalletFragment();
        fragmentTransaction.replace(binding.fragLayout.getId(), walletFragment);
        fragmentTransaction.commit();
    }


    /**
     * 登录后更新用户信息
     */
    public void getUserInfo() {
        new Thread(() -> {
            Type type = new TypeToken<LzyResponse<UserInfoDataModel>>() {
            }.getType();
            APIService.<LzyResponse<UserInfoDataModel>>getData(type, Urls.USER_DATE_INFO).subscribeOn(Schedulers.io()).map(response -> response.data).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<UserInfoDataModel>() {
                @Override
                public void onSubscribe(Disposable d) {
                    Log.e("onSubscribe ： " + d);
                }

                @SuppressLint("NotifyDataSetChanged")
                @Override
                public void onNext(UserInfoDataModel userInfoDataModel) {
                    if (userInfoDataModel == null) {
                        return;
                    }
                    Glide.with(MainActivity.this).load(userInfoDataModel.getUser_info().getAvatar()).centerCrop().into(binding.userLayout);
                    CashUtil.getInstance().setWorksState(userInfoDataModel.getUser_info().getStudio_id());
                    SpCashUtils.getInstance().setUserId(userInfoDataModel.getUser_info().getId());
                    if (userInfoDataModel.getUser_info().getStudio_id() > 0) {
                        SpCashUtils.getInstance().setWorkId(userInfoDataModel.getUser_info().getStudio_id());
                    }
                    EventBusUtil.sendEvent(new EventMessage(EventContent.updateState));
                }

                @Override
                public void onError(Throwable e) {
                    e.printStackTrace();            //请求失败
                    Log.e("onError ： " + "获取失败");
                }

                @Override
                public void onComplete() {
                }
            }); //
        }).start();
    }


    private WorkDialogHelper.NetworkCHangeReceive networkCHangeReceive;


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == WorkDialogHelper.REQUEST_SELECT_IMAGES_CODE && resultCode == RESULT_OK) {
            List<MediaFile> mediaFiles = (List<MediaFile>) data.getSerializableExtra(ImagePicker.EXTRA_SELECT_IMAGES);
            final String Intent_Action = "com.android.BroadcastReceiver";//定义广播，方便我们接收这个广播
            Intent intent = new Intent(Intent_Action);
            intent.putExtra("mediaFiles", (Serializable) mediaFiles);
            sendBroadcast(intent);
//            EventBusUtil.sendEvent(new EventListMessage(EventContent.Pic_Video,mediaFiles));
        } else if(requestCode == 100) {
            startService(new Intent(this, BottomDiscService.class));
        }
    }

    @SuppressLint({"NewApi", "UseCompatLoadingForDrawables", "NotifyDataSetChanged"})
    public void disBg(int type) {
        for (LeftMenuModel leftMenuModel : leftData) {
            leftMenuModel.setSelect(false);
        }
        leftMenuAdapter.notifyDataSetChanged();
        switch (type) {
            case 1://学院
                binding.fitnessCollegeTv.setBackground(this.getDrawable(R.drawable.btn_ty2));
                binding.fitnessCollegeTv.setTextColor(this.getColor(R.color.white));
                binding.useGettingStartedTv.setBackground(this.getDrawable(R.color.back_main));
                binding.useGettingStartedTv.setTextColor(this.getColor(R.color.gray_text1));
                binding.btnNumRs.setBackground(this.getDrawable(R.color.back_main));
                binding.btnNumRs.setTextColor(this.getColor(R.color.gray_text1));
                binding.allGame.setBackground(this.getDrawable(R.color.back_main));
                binding.allGame.setTextColor(this.getColor(R.color.gray_text1));
                //底部
                binding.followCl.setBackground(this.getDrawable(R.drawable.cirecle_back));
                binding.messageTv.setBackground(this.getDrawable(R.drawable.cirecle_back));
                binding.orderTv.setBackground(this.getDrawable(R.drawable.cirecle_back));
                binding.wallet.setBackground(this.getDrawable(R.drawable.cirecle_back));
                break;
            case 2://使用入门
                binding.fitnessCollegeTv.setBackground(this.getDrawable(R.color.back_main));
                binding.fitnessCollegeTv.setTextColor(this.getColor(R.color.gray_text1));
                binding.useGettingStartedTv.setBackground(this.getDrawable(R.drawable.btn_ty2));
                binding.useGettingStartedTv.setTextColor(this.getColor(R.color.white));
                binding.btnNumRs.setBackground(this.getDrawable(R.color.back_main));
                binding.btnNumRs.setTextColor(this.getColor(R.color.gray_text1));
                binding.allGame.setBackground(this.getDrawable(R.color.back_main));
                binding.allGame.setTextColor(this.getColor(R.color.gray_text1));
                //底部
                binding.followCl.setBackground(this.getDrawable(R.drawable.cirecle_back));
                binding.messageTv.setBackground(this.getDrawable(R.drawable.cirecle_back));
                binding.orderTv.setBackground(this.getDrawable(R.drawable.cirecle_back));
                binding.wallet.setBackground(this.getDrawable(R.drawable.cirecle_back));
                break;
            case 3:
                binding.fitnessCollegeTv.setBackground(this.getDrawable(R.color.back_main));
                binding.fitnessCollegeTv.setTextColor(this.getColor(R.color.gray_text1));
                binding.useGettingStartedTv.setBackground(this.getDrawable(R.color.back_main));
                binding.useGettingStartedTv.setTextColor(this.getColor(R.color.gray_text1));
                binding.btnNumRs.setBackground(this.getDrawable(R.color.back_main));
                binding.btnNumRs.setTextColor(this.getColor(R.color.gray_text1));
                binding.allGame.setBackground(this.getDrawable(R.color.back_main));
                binding.allGame.setTextColor(this.getColor(R.color.gray_text1));
                //底部
                binding.followCl.setBackground(this.getDrawable(R.drawable.cirecle_back));
                binding.messageTv.setBackground(this.getDrawable(R.drawable.cirecle_back));
                binding.orderTv.setBackground(this.getDrawable(R.drawable.cirecle_back));
                binding.wallet.setBackground(this.getDrawable(R.drawable.cirecle_back));
                break;
            case 4:
                binding.fitnessCollegeTv.setBackground(this.getDrawable(R.color.back_main));
                binding.fitnessCollegeTv.setTextColor(this.getColor(R.color.gray_text1));
                binding.useGettingStartedTv.setBackground(this.getDrawable(R.color.back_main));
                binding.useGettingStartedTv.setTextColor(this.getColor(R.color.gray_text1));
                binding.btnNumRs.setBackground(this.getDrawable(R.color.back_main));
                binding.btnNumRs.setTextColor(this.getColor(R.color.gray_text1));
                binding.allGame.setBackground(this.getDrawable(R.color.back_main));
                binding.allGame.setTextColor(this.getColor(R.color.gray_text1));
                //底部
                binding.followCl.setBackground(this.getDrawable(R.drawable.cirecle_back2));
                binding.messageTv.setBackground(this.getDrawable(R.drawable.cirecle_back));
                binding.orderTv.setBackground(this.getDrawable(R.drawable.cirecle_back));
                binding.wallet.setBackground(this.getDrawable(R.drawable.cirecle_back));
                break;
            case 5:
                binding.fitnessCollegeTv.setBackground(this.getDrawable(R.color.back_main));
                binding.fitnessCollegeTv.setTextColor(this.getColor(R.color.gray_text1));
                binding.useGettingStartedTv.setBackground(this.getDrawable(R.color.back_main));
                binding.useGettingStartedTv.setTextColor(this.getColor(R.color.gray_text1));
                binding.btnNumRs.setBackground(this.getDrawable(R.color.back_main));
                binding.btnNumRs.setTextColor(this.getColor(R.color.gray_text1));
                binding.allGame.setBackground(this.getDrawable(R.color.back_main));
                binding.allGame.setTextColor(this.getColor(R.color.gray_text1));
                //底部
                binding.followCl.setBackground(this.getDrawable(R.drawable.cirecle_back));
                binding.messageTv.setBackground(this.getDrawable(R.drawable.cirecle_back2));
                binding.orderTv.setBackground(this.getDrawable(R.drawable.cirecle_back));
                binding.wallet.setBackground(this.getDrawable(R.drawable.cirecle_back));
                break;
            case 6:
                binding.fitnessCollegeTv.setBackground(this.getDrawable(R.color.back_main));
                binding.fitnessCollegeTv.setTextColor(this.getColor(R.color.gray_text1));
                binding.useGettingStartedTv.setBackground(this.getDrawable(R.color.back_main));
                binding.useGettingStartedTv.setTextColor(this.getColor(R.color.gray_text1));
                binding.btnNumRs.setBackground(this.getDrawable(R.color.back_main));
                binding.btnNumRs.setTextColor(this.getColor(R.color.gray_text1));
                binding.allGame.setBackground(this.getDrawable(R.color.back_main));
                binding.allGame.setTextColor(this.getColor(R.color.gray_text1));
                //底部
                binding.followCl.setBackground(this.getDrawable(R.drawable.cirecle_back));
                binding.messageTv.setBackground(this.getDrawable(R.drawable.cirecle_back));
                binding.orderTv.setBackground(this.getDrawable(R.drawable.cirecle_back2));
                binding.wallet.setBackground(this.getDrawable(R.drawable.cirecle_back));
                break;
            case 7:
                binding.fitnessCollegeTv.setBackground(this.getDrawable(R.color.back_main));
                binding.fitnessCollegeTv.setTextColor(this.getColor(R.color.gray_text1));
                binding.useGettingStartedTv.setBackground(this.getDrawable(R.color.back_main));
                binding.useGettingStartedTv.setTextColor(this.getColor(R.color.gray_text1));
                binding.btnNumRs.setBackground(this.getDrawable(R.drawable.btn_ty2));
                binding.btnNumRs.setTextColor(this.getColor(R.color.back_main));
                binding.allGame.setBackground(this.getDrawable(R.color.back_main));
                binding.allGame.setTextColor(this.getColor(R.color.gray_text1));
                //底部
                binding.followCl.setBackground(this.getDrawable(R.drawable.cirecle_back));
                binding.messageTv.setBackground(this.getDrawable(R.drawable.cirecle_back));
                binding.orderTv.setBackground(this.getDrawable(R.drawable.cirecle_back));
                binding.wallet.setBackground(this.getDrawable(R.drawable.cirecle_back));
                break;
            case 8:
                binding.fitnessCollegeTv.setBackground(this.getDrawable(R.color.back_main));
                binding.fitnessCollegeTv.setTextColor(this.getColor(R.color.gray_text1));
                binding.useGettingStartedTv.setBackground(this.getDrawable(R.color.back_main));
                binding.useGettingStartedTv.setTextColor(this.getColor(R.color.gray_text1));
                binding.btnNumRs.setBackground(this.getDrawable(R.color.back_main));
                binding.btnNumRs.setTextColor(this.getColor(R.color.gray_text1));
                binding.allGame.setBackground(this.getDrawable(R.drawable.btn_ty2));
                binding.allGame.setTextColor(this.getColor(R.color.back_main));
                //底部
                binding.followCl.setBackground(this.getDrawable(R.drawable.cirecle_back));
                binding.messageTv.setBackground(this.getDrawable(R.drawable.cirecle_back));
                binding.orderTv.setBackground(this.getDrawable(R.drawable.cirecle_back));
                binding.wallet.setBackground(this.getDrawable(R.drawable.cirecle_back));
                break;
            case 9:
                binding.fitnessCollegeTv.setBackground(this.getDrawable(R.color.back_main));
                binding.fitnessCollegeTv.setTextColor(this.getColor(R.color.gray_text1));
                binding.useGettingStartedTv.setBackground(this.getDrawable(R.color.back_main));
                binding.useGettingStartedTv.setTextColor(this.getColor(R.color.gray_text1));
                binding.btnNumRs.setBackground(this.getDrawable(R.color.back_main));
                binding.btnNumRs.setTextColor(this.getColor(R.color.gray_text1));
                binding.allGame.setBackground(this.getDrawable(R.color.back_main));
                binding.allGame.setTextColor(this.getColor(R.color.gray_text1));
                //底部
                binding.followCl.setBackground(this.getDrawable(R.drawable.cirecle_back));
                binding.messageTv.setBackground(this.getDrawable(R.drawable.cirecle_back));
                binding.orderTv.setBackground(this.getDrawable(R.drawable.cirecle_back));
                binding.wallet.setBackground(this.getDrawable(R.drawable.cirecle_back2));
                break;
        }

    }


    public static int bigEndianToLittleEndian(byte[] bigEndianBytes) {
        ByteBuffer buffer = ByteBuffer.wrap(bigEndianBytes);
        buffer.order(ByteOrder.LITTLE_ENDIAN);
        return buffer.getInt();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onDataReceived(byte[] data) {
        String receivedData = new String(data);
        ToastUtil.toastShortMessage("USBSerialHelper" + " Received data: " + receivedData);
        Log.e("USBSerialHelper", "Received data: " + receivedData);
    }

    @Override
    public void doSomethingInActivity() {

    }
}
